package com.ucap.weking.guangan.views;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.CardView;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.ucap.weking.guangan.Activitys.NewContentActivity;
import com.ucap.weking.guangan.R;
import com.ucap.weking.guangan.modle.LeaderNameModel;

import org.w3c.dom.Text;

import java.util.ArrayList;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * Created by weking on 2017/10/23.
 */

public class LeaderItem extends LinearLayout {

    @InjectView(R.id.rv_content)
    RecyclerView rvContent;
    @InjectView(R.id.tv_ty_name)
    TextView tvType;

    public LeaderItem(Context context) {
        super(context);
        addContent();
    }

    public LeaderItem(Context context, AttributeSet attrs) {
        super(context, attrs);
        addContent();
    }

    public LeaderItem(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        addContent();
    }

    public void setData(ArrayList<LeaderNameModel> data) {
        if (data != null) {
            View inflate = LayoutInflater.from(getContext()).inflate(R.layout.item_leader, null);
            ButterKnife.inject(this, inflate);
            this.addView(inflate);
            GridLayoutManager manager = new GridLayoutManager(getContext(), 3);
            rvContent.setHasFixedSize(true);
            rvContent.setNestedScrollingEnabled(false);
            rvContent.setLayoutManager(manager);
            rvContent.setAdapter(new Adapter(data));
            tvType.setText(data.get(0).getTypeName());
        }

    }

    public void addContent() {

    }


    class Adapter extends RecyclerView.Adapter<Holder> {
        ArrayList<LeaderNameModel> data;

        public Adapter(ArrayList<LeaderNameModel> data) {
            this.data = data;
        }

        @Override
        public Holder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = View.inflate(parent.getContext(), R.layout.item_leader_name, null);
            return new Holder(view);
        }

        @Override
        public void onBindViewHolder(Holder holder, int position) {
            final LeaderNameModel leaderNameModel = data.get(position);
            Glide.with(getContext())
                    .load(leaderNameModel.getPhoto())
                    .into(holder.ivPhoto);
            holder.tvName.setText(leaderNameModel.getName());
            holder.cvRoot.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    Log.i("leaderContentUri", "leaderContentUri: " + leaderNameModel.getContentUri());
                    Intent intent = new Intent(getContext(), NewContentActivity.class);
                    intent.putExtra("title", "政府领导");
                    intent.putExtra("uri", leaderNameModel.getContentUri());
                    getContext().startActivity(intent);
                }
            });
        }

        @Override
        public int getItemCount() {
            return data.size();
        }
    }

    class Holder extends RecyclerView.ViewHolder {
        @InjectView(R.id.tv_name)
        TextView tvName;
        @InjectView(R.id.iv_photo)
        ImageView ivPhoto;
        @InjectView(R.id.cd_root)
        CardView cvRoot;

        public Holder(View itemView) {
            super(itemView);
            ButterKnife.inject(this, itemView);
        }
    }
}
